package com.scala.learn.sparkSqlDB

import org.apache.spark.sql.{DataFrame, Dataset, Row, SparkSession}

/**
  * @Copyright: Shanghai Definesys Company.All rights reserved.
  * @Description:1、从数据库中读取数据
  *               2、写入数据库
  * @author: chuhaitao
  * @since: 2019/3/16 14:46
  * @history:
  *          1.2019/3/16 created by chuhaitao
  */
object SparkSqlMySql {


  def main(args: Array[String]): Unit = {
    val spark: SparkSession = SparkSession.builder()
      .appName("MySql")
      .master("local")
      .getOrCreate()

    val mysqlDatas: DataFrame = spark.read.format("jdbc").options(
      Map("url" -> "jdbc:mysql://localhost:3306/test",
        "driver" -> "com.mysql.jdbc.Driver",
        "dbtable" -> "user",
        "user" -> "root",
        "password" -> "123456"

      )


    ).load()
    mysqlDatas.show()
    mysqlDatas
      .write.mode("append").format("jdbc").options(
      Map("url" -> "jdbc:mysql://localhost:3306/test2",
        "driver" -> "com.mysql.jdbc.Driver",
        "dbtable" -> "user",
        "user" -> "root",
        "password" -> "123456"

      )


    )


  }
}
